
<template>
  <div class="filter-container">
    <el-form ref="form" :model="formMapping" label-width="80px">
      <el-form-item label="映射方式：" class="margin-bottom-0">
        <el-radio v-model="formMapping.mappingType" label="simple" @change="mappingTypeChange">简单值</el-radio>
        <el-radio v-model="formMapping.mappingType" label="dataset" @change="mappingTypeChange">数据集</el-radio>
      </el-form-item>
    </el-form>
    <!--简单值配置-->
    <div v-if="formMapping.mappingType==='simple'">
      <el-table :data="mappingItems" style="width: 100%" size="mini" max-height="450px">
        <el-table-column prop="value" label="实际值">
        </el-table-column>
        <el-table-column prop="label" label="显示值">
        </el-table-column>
        <el-table-column prop="operation" label="操作" width="120">
          <template slot-scope="{row, column}">
            <el-button type="text" size="small" @click="mappingEdit(row)">编辑</el-button>
            <el-button type="text" size="small" @click="mappingDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" class="margin-top-10" @click="mappingAdd">添加</el-button>
    </div>
    <!--数据集映射配置-->
    <div v-if="formMapping.mappingType==='dataset'">
      <el-form ref="form" :model="formMapping" label-width="130px">
        <el-form-item label="数据集：">
          <el-select v-model="formMapping.mappingDataset" placeholder="请选择数据集" class="w-100pc" @change="mappingDatasetChange">
            <el-option v-for="item in datasets" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际值属性：">
          <el-select v-model="formMapping.mappingKeyProperty" placeholder="请选择实际值属性" class="w-100pc" @change="mappingDatasetChange">
            <el-option v-for="(item, index) in fields" :key="index" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示值属性：">
          <el-select v-model="formMapping.mappingValueProperty" placeholder="请选择显示值属性" class="w-100pc" @change="mappingDatasetChange">
            <el-option v-for="(item, index) in fields" :key="index" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>

    <!--数据映射项对话框-->
    <el-dialog v-dialogDrag title="数据映射项" :visible.sync="showMapptingDialog" width="500px" append-to-body>
      <el-form ref="form" :model="currentMapping" label-width="130px">
        <el-form-item label="实际值：">
          <el-input v-model="currentMapping.value" class="w-100pc" placeholder="请输入实际值"></el-input>
        </el-form-item>
        <el-form-item label="显示值：">
          <el-input v-model="currentMapping.label" class="w-100pc" placeholder="请输入显示值"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showMapptingDialog = false">取消</el-button>
        <el-button type="primary" @click="saveMappingItem">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import propertyHelper from "../components/propertyHelper.js";
import toolHelper from "../tools/toolHelper.js";

export default {
  name: "filter-condition-tab",
  props: {
    designer: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data () {
    return {
      fields: [], // 字段集合
      toolHelper,
      propertyHelper,
      formData: {
        fileName: null,
        prefix: null
      },
      // 显示条件配置对话框
      showMapptingDialog: false,
      // 当前选中mapping项
      currentMapping: {},
      // 条件配置表单
      formMapping: {
        mappingType: "simple",
        mappingDataset: null,
        mappingKeyProperty: null,
        mappingValueProperty: null
      },
      // 简单值映射值列表
      mappingItems: []
    };
  },
  computed: {
    datasets: function () {
      const { context } = this.designer;
      if (!context) return [];

      const datasources = context.reportDef.datasources;
      const sets = [];
      for (const ds of datasources) {
        const datasets = ds.datasets || [];
        for (const dataset of datasets) {
          sets.push(dataset.name);
        }
      }
      this.getFields(); // 获取字段集合

      return sets;
    }
  },
  watch: {
    "designer.cellDef": {
      handler: function () {
        this.dataInit();
      },
      deep: true
    }
  },
  methods: {
    // 获取字段集合
    getFields () {
      const { context } = this.designer;
      const datasources = context.reportDef.datasources;
      let fields = [];
      const dsName = this.formMapping.mappingDataset;
      for (const ds of datasources) {
        const datasets = ds.datasets || [];
        for (const dataset of datasets) {
          if (dataset.name === dsName) {
            fields = dataset.fields || [];
            break;
          }
        }
        if (fields.length > 0) {
          break;
        }
      }

      this.fields = fields;
    },
    // 初始化数据
    dataInit () {
      const { cellDef } = this.designer;
      if (!cellDef || !cellDef.value) return;

      this.formMapping.mappingType = cellDef.value.mappingType;

      if (this.formMapping.mappingType === "simple") {
        if (!Array.isArray(cellDef.value.mappingItems)) {
          cellDef.value.mappingItems = [];
        }
        this.mappingItems = cellDef.value.mappingItems;
        this.formMapping.mappingDataset = null;
        this.formMapping.mappingKeyProperty = null;
        this.formMapping.mappingValueProperty = null;
      } else {
        this.mappingItems = null;
        this.formMapping.mappingDataset = cellDef.value.mappingDataset;
        this.formMapping.mappingKeyProperty = cellDef.value.mappingKeyProperty;
        this.formMapping.mappingValueProperty = cellDef.value.mappingValueProperty;
      }
    },
    // 映射方式改变
    mappingTypeChange () {
      const { cellDef } = this.designer;
      cellDef.value.mappingType = this.formMapping.mappingType;
    },
    // 新增条件
    mappingAdd () {
      this.currentMapping = {};
      this.showMapptingDialog = true;
    },
    // 编辑条件
    mappingEdit (row) {
      this.currentMapping = row;
      this.showMapptingDialog = true;
    },
    // 删除条件
    mappingDelete (row) {
      this.$confirm("此操作删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const index = this.mappingItems.findIndex(item => item === row);
          this.mappingItems.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 保存条件
    saveMappingItem () {
      if (!this.currentMapping.value) {
        this.$message.error("实际值不能为空！");
        return;
      }
      if (!this.currentMapping.label) {
        this.$message.error("显示值不能为空！");
        return;
      }
      const index = this.mappingItems.findIndex(item => item === this.currentMapping);
      if (index < 0) {
        // 新增条件
        this.mappingItems.push(this.currentMapping);
      }
      this.showMapptingDialog = false;
    },
    // 数据集改变
    mappingDatasetChange () {
      const { cellDef } = this.designer;
      cellDef.value.mappingDataset = this.formMapping.mappingDataset;
      cellDef.value.mappingKeyProperty = this.formMapping.mappingKeyProperty;
      cellDef.value.mappingValueProperty = this.formMapping.mappingValueProperty;
    }
  }
};
</script>

<style lang="scss" scoped>
.filter-container {
  .cond-center {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    width: 320px;
    margin-left: 10px;
  }

  .header-box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #dcdfe6;
    padding: 10px;
    .title {
      padding: 0;
    }
  }
  .item-box {
    padding: 0;
    .item {
      padding: 5px 10px;
      line-height: 22px;
      border-bottom: 1px solid #dcdfe6;
      cursor: pointer;
      &.enter {
        background-color: #409eff;
        color: white;
      }
      .txt {
        padding-right: 5px;
      }
    }
  }
  .no-data {
    text-align: center;
    color: #666;
    padding: 20px 0;
  }
}
</style>
